package com.example.qxfw.backgroundManagement.controller;

import com.example.qxfw.backgroundManagement.service.AddressManagementService;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.GroupInfo;
import com.example.qxfw.common.entity.Liaison;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZeLin Wang
 * @version 1.0
 * @description: 后台管理-通讯录管理
 * @date 2024/7/3 10:12
 */
@Slf4j
@Api(tags = "后台管理-通讯录管理")
@RestController
@CrossOrigin
@RequestMapping("/addressManagement")
public class AddressManagementController {

    @Autowired
    private AddressManagementService addressManagementService;

    @ApiOperation("初始化组群列表")
    @GetMapping(value = "selectAllGroupInfo")
    public SysResult selectAllGroupInfo() {
        List<GroupInfo> groupInfos = this.addressManagementService.selectAllGroup();
        return SysResult.success(groupInfos);
    }

    @ApiOperation("初始化联系人列表")
    @GetMapping(value = "selectAllLiaisonInfo")
    public SysResult selectAllLiaisonInfo() {
        List<Liaison> liaisonInfos = this.addressManagementService.selectAllLiaison();
        return SysResult.success(liaisonInfos);
    }

    @ApiOperation("添加组群")
    @PostMapping(value = "addGroupInfo")
    public SysResult addGroupInfo(String groupName) {
        Integer addGroupInfo = this.addressManagementService.addGroupInfo(groupName);
        return returnInformation("",addGroupInfo);
    }

    @ApiOperation("删除组群")
    @PostMapping(value = "deleteGroupInfo")
    public SysResult deleteGroupInfo(int id) {
        Integer delGroupInfo = this.addressManagementService.deleteGroupInfo(id);
        return returnInformation("",delGroupInfo);
    }

    @ApiOperation("添加联系人")
    @PostMapping(value = "addLiaisonInfo")
    public SysResult addLiaisonInfo(@RequestBody Liaison liaison) {
        Integer addLiaisonInfo = this.addressManagementService.addLiaisonInfo(liaison);
        return returnInformation("",addLiaisonInfo);
    }

    @ApiOperation("修改联系人")
    @PostMapping(value = "updateLiaisonInfo")
    public SysResult updateLiaisonInfo(int id,@RequestBody Liaison liaison) {
        liaison.setId(id);
        Integer updateLiaisonInfo = this.addressManagementService.updateLiaisonInfo(liaison);
        return returnInformation("",updateLiaisonInfo);
    }

    @ApiOperation("删除联系人")
    @PostMapping(value = "delLiaisonInfo")
    public SysResult delLiaisonInfo(int id) {
        Integer delLiaisonInfo = this.addressManagementService.delLiaisonInfo(id);
        return returnInformation("",delLiaisonInfo);
    }

    public SysResult returnInformation(String info, Integer result) {
        if (result < 0) {
            return SysResult.fail(info + "失败");
        } else {
            return SysResult.success(result);
        }
    }


}